<?php

namespace App\Http\Controllers;

use App\Models\ClassifyTemplate;
use App\Models\DossierCombination;
use App\Services\CommonService;
use App\Services\DossierCombinationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * 案卷构成规范
 * Class DossierCombinationController
 * @package App\Http\Controllers
 */
class DossierCombinationController extends Controller
{
    public $dossierCombinationService;

    public function __construct(DossierCombinationService $dossierCombinationService)
    {
        parent::__construct();
        $this->dossierCombinationService = $dossierCombinationService;
    }

    /**
     * 数据列表
     */
    public function index(Request $request): JsonResponse
    {
        $keyword = $request->input('keyword');
        $perPage = $request->input('per_page') ?: $this->limit;

        $list = $this->dossierCombinationService->getList($perPage, $keyword);

        return $this->success($list);
    }

    /**
     * 添加一个案卷模板规范
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/2 16:49
     */
    public function store(Request $request) : JsonResponse
    {
        if (!$this->dossierCombinationService->saveData($request->all())) {
            return $this->error('保存失败');
        }

        return $this->success('', '保存成功！');
    }

    /**
     * 获取案卷构成模板
     */
    public function getTemplates(Request $request)
    {
        $templates = $this->dossierCombinationService->getTemplates();

        return $this->success($templates);
    }

    /**
     * 单个软删除 （软删除相关的全部是Laravel内置操作方法）
     */
    public function destroy(Request $request)
    {
        $id = $request->input('id', 0);
        if (!DossierCombination::query()->where('id', $id)->delete()) {
            return $this->error('删除失败！');
        };
        CommonService::addActionLog('单个软删案卷构成规范', "数据ID{$id}");

        return $this->success('', '删除成功');
    }

    /**
     * 恢复单个数据
     */
    function retrieve(Request $request)
    {
        $id = $request->input('id', 0);
        if (!DossierCombination::query()->where('id', $id)->restore()) {
            return $this->error('恢复失败！');
        }
        CommonService::addActionLog('恢复案卷构成规范', "数据ID:{$id}");

        return $this->success('', '恢复成功');
    }

    /**
     * 回收站列表（进行软删除的数据）
     */
    public function recycle(Request $request)
    {
        $data = $this->DossierCombinationService->getRecycles($request);

        return $this->success($data);
    }


}
